package com.ht.baselibs.ui.base;

import android.app.ProgressDialog;

import com.ht.baselibs.bean.ApkInfoBean;
import com.ht.baselibs.bean.GetBean;
import com.ht.baselibs.constant.SPConfig;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.manager.BaseLibApiManager;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.utils.DeviceUtils;
import com.ht.baselibs.utils.LogUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.ht.baselibs.utils.UpdateDialogUtils;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by huangtao on 16/9/14.
 */
public abstract class BaseCheckUpdateActivity extends BaseActivity {

    private ProgressDialog mUpdateDialog;
    private boolean isForcedUpdate;

    @Override
    protected void initUpdate() {
        String checkUpdateUrl = getCheckUpdateUrl();
        if (checkUpdateUrl != null) {
            Call<GetBean<ApkInfoBean>> checkUpdate = BaseLibApiManager.getApi().checkUpdate(checkUpdateUrl);
            checkUpdate.enqueue(new NetCallBack<GetBean<ApkInfoBean>>() {
                @Override
                public void onSuccess(Call<GetBean<ApkInfoBean>> call, Response<GetBean<ApkInfoBean>> response) {
                    GetBean<ApkInfoBean> getBean = response.body();
                    if (getBean.code == getSuccessCode()) {
                        ApkInfoBean apkInfoBean = getBean.data;
                        if (DeviceUtils.getAppVersionCode() < apkInfoBean.versionCode) {
                            String newVersionName = apkInfoBean.versionName;
                            String ignoreVersionName = SharedPreferencesUtils.getString(SPConfig.IGNORE_APP_UPDATE_VERSION, "");
                            if ("".equals(ignoreVersionName) || !ignoreVersionName.equals(newVersionName)) {
                                String[] versionStr = newVersionName.split("\\.");

                                isForcedUpdate = versionStr.length >= UpdateDialogUtils.FORCED_UPDATE_LENG;

                                mUpdateDialog = new ProgressDialog(BaseCheckUpdateActivity.this);
                                mUpdateDialog.setTitle("下载最新安装包");
                                mUpdateDialog.setMessage("正在下载，请稍后...");
                                mUpdateDialog.setMax(100);
                                mUpdateDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                                UpdateDialogUtils.showUpdateDialog(BaseCheckUpdateActivity.this, apkInfoBean, mUpdateDialog);
                            }
                        }
                    } else {
                        UIUtils.showToast(getBean.msg);
                    }
                }

                @Override
                public void onFailure(Call<GetBean<ApkInfoBean>> call, Throwable t) {
                    LogUtils.error(t.getMessage());
                }
            });
        }
    }


    protected abstract String getCheckUpdateUrl();

    protected abstract int getSuccessCode();

    @Override
    protected void onEvent(int code, Object data) {
        switch (code){
            case EventCode.EVENT_CODE_UPDATE_APK_PROGRESS:
                mUpdateDialog.setProgress((Integer) data);
                break;
            case EventCode.EVENT_CODE_UPDATE_APK_END:
                DeviceUtils.installApk(this, (String)data);
                if (!isForcedUpdate){
                    mUpdateDialog.dismiss();
                }
                break;
            case EventCode.EVENT_CODE_UPDATE_APK_ERROR:
                UIUtils.showToast("您当前的网络不稳定，下载安装包失败");
                mUpdateDialog.dismiss();
                break;
        }
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }
}
